package com.sunda.spmsweb.overseacontroller;


import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.OverseaWhsOutDTO;
import com.sunda.spmsoversea.dto.OverseaWhsOutGenerateDTO;
import com.sunda.spmsoversea.dto.OverseaWhsOutQueryDTO;
import com.sunda.spmsoversea.service.IOverseaWhsOutService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 海外出库任务表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-09-22
 */
@RestController
@RequestMapping("/overseaWhsOut")
@Api(tags = "海外仓出库任务", description = "海外仓出库任务")
public class OverseaWhsOutController {

    @Autowired
    IOverseaWhsOutService iOverseaWhsOutService;

    @RequestMapping("/generateOverseaWhsOut")
    @ApiOperation(value = "根据领用申请创建出库任务", notes = "根据领用申请创建出库任务\n" +
            "示例参数：\n" +
            "1.校验传入参数合法；校验领用申请及明细数据合法；领用申请每行明细 物料实际移动类型 必须有值；\n" +
            "2.根据领用申请实际值更新领用申请明细数据；\n" +
            "3.遍历更新后的领用出库所有明细行，相同的 实际移动类型 放到同一个出库任务中；\n" +
            "4.根据领用申请明细批准数据大于0的行生成出库任务明细数据；\n" +
            "5.更新领用申请表头状态；\n" +
            "6.记录领用申请表头更新日志；记录出库任务生成日志；", httpMethod = "POST")
    public ResponseResult generateOverseaWhsOut(@RequestBody OverseaWhsOutGenerateDTO overseaWhsOutGenerateDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsOutService.generateOverseaWhsOut(overseaWhsOutGenerateDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getWhsOutPage")
    @ApiOperation(value = "获取出库任务表头分页", notes = "获取出库任务表头分页\n" +
            "单据状态说明：单据状态(0已删除;1出库中;2出库完成;3提交SAP成功;4提交SAP失败;5已撤销;)\n" +
            "relateDocument 关联单据号，在出库任务中即为 领用申请单号；\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"beginDate\": \"\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"endDate\": \"\",\n" +
            "\t\"movementType\": \"\",\n" +
            "\t\"size\": 20,\n" +
            "\t\"spmsId\": \"\",\n" +
            "\t\"relateDocument\": \"TF01-EL-20220223-001\",\n" +
            "\t\"comments\": \"膨胀螺丝\",\n" +
            "\t\"createUser\": \"\",\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"uuidWhsOut\": \"\",\n" +
            "\t\"werks\": \"\",\n" +
            "\t\"whsLocationCode\": \"\",\n" +
            "\t\"workshopCode\": \"\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult getWhsOutPage(@RequestBody OverseaWhsOutQueryDTO overseaWhsOutQueryDTO){
        try {
            return ResponseResult.success().add("whsOutList", iOverseaWhsOutService.getWhsOutPage(overseaWhsOutQueryDTO));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getWhsOutAndDtl")
    @ApiOperation(value = "获取一条出库任务及其明细", notes = "获取出库任务及其明细\n" +
            "示例参数：uuidWhsOut = 2df05cc85526431999fbd2f6ca7ac1d3, 88d14077a025415b8be468666de2719b\n" +
            "", httpMethod = "POST")
    public ResponseResult getWhsOutAndDtl(@RequestParam String uuidWhsOut){
        try {
            return iOverseaWhsOutService.getWhsOutAndDtl(uuidWhsOut);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/closeWhsOut")
    @ApiOperation(value = "出库任务关闭", notes = "出库任务关闭\n" +
            "单据状态说明：单据状态(0已删除;1出库中;2出库完成;3提交SAP成功;4提交SAP失败;5已撤销;)\n" +
            "此接口包含状态变化：1-0；\n" +
            "示例参数：uuidWhsOut = 2df05cc85526431999fbd2f6ca7ac1d3, 88d14077a025415b8be468666de2719b\n" +
            "", httpMethod = "POST")
    public ResponseResult closeWhsOut(@RequestParam String uuidWhsOut, @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsOutService.closeWhsOut(uuidWhsOut, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/updateWhsOut")
    @ApiOperation(value = "更新出库任务及明细", notes = "更新出库任务及明细\n" +
            "单据状态说明：单据状态(0已删除;1出库中;2出库完成;3提交SAP成功;4提交SAP失败;5已撤销;)\n" +
            "此接口包含状态变化：1-1；1-2(扣库存)；\n" +
            "示例参数：\n" +
            "", httpMethod = "POST")
    public ResponseResult updateWhsOut(@RequestBody OverseaWhsOutDTO overseaWhsOutDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsOutService.updateWhsOut(overseaWhsOutDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/toSapWhsOut")
    @ApiOperation(value = "出库任务提交SAP", notes = "出库任务提交SAP\n" +
            "单据状态说明：单据状态(0已删除;1出库中;2出库完成;3提交SAP成功;4提交SAP失败;5已撤销;)\n" +
            "此接口包含状态变化：2-3；2-4；4-3；4-4；\n" +
            "示例参数：\n" +
            "", httpMethod = "POST")
    public ResponseResult toSapWhsOut(@RequestParam String uuidWhsOut,
                                      @RequestParam Integer dataVersion,
                                      @RequestParam String postingDate){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsOutService.toSapWhsOut(uuidWhsOut, dataVersion, postingDate, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/cancelWhsOut")
    @ApiOperation(value = "出库任务撤销/退回操作", notes = "出库任务撤销/退回操作\n" +
            "单据状态说明：单据状态(0已删除;1出库中;2出库完成;3提交SAP成功;4提交SAP失败;5已撤销;)\n" +
            "此接口包含状态变化：3-2；2-1(加库存)；4-1(加库存)；\n" +
            "示例参数：\n" +
            "", httpMethod = "POST")
    public ResponseResult cancelWhsOut(@RequestParam String uuidWhsOut,
                                       @RequestParam Integer dataVersion, String cancelPostingDate){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsOutService.cancelWhsOut(uuidWhsOut, dataVersion, cancelPostingDate, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

}
